package pandabay2.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pandabay2.DAO.AuctionDAO;
import pandabay2.domain.Auction;
import pandabay2.domain.User;

/**
 * Servlet implementation class BuyNowServlet
 */
public class BuyNowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuyNowServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/** 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// see if it is a logged-in user
		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute("currentSessionUser");
		if(user == null) {
		response.sendRedirect("LoginPage.jsp");
		}
		try {
			System.out.print("in the Buy Now Servlet");
			int auction_id = Integer.parseInt(request.getParameter("id"));
			Auction auction = AuctionDAO.readAuction(auction_id);
			request.setAttribute("auction", auction);
			int buyer = user.getUser_id();
			AuctionDAO.setBuyer(auction_id, buyer);
		request.getRequestDispatcher("/AuctionWon.jsp").forward(request, response);
	} catch (Throwable exc) {
		System.out.println(exc);
	}
}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

}
